package com.example.rs485demo.util;
 
/**
 * Demo class
 *
 * @author XDRS
 * @date 2023/3/30 16:01
 */
 
//import gnu.io.SerialPortEvent;
//import gnu.io.SerialPortEventListener;
import net.sf.json.JSONObject;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Date;
 
public class MyLister implements SerialPortEventListener {
 
    @Override
    public void serialEvent(SerialPortEvent arg0) {
        try {
            if (arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                SerialPortUtil serialPortUtil = SerialPortUtil.getSerialPortUtil();
                byte[] bytes = serialPortUtil.readFromPort(PortInit.serialPort);
                String needData = printHexString(bytes);
                boolean b = needData.contains("01 03");
                String[] strings = needData.split(" ");
                int xxxx = Integer.parseInt(strings[0] + "", 16);
                if (xxxx == 1 && b) {
                    System.out.println(new Date() + "【字节数组转16进制字符串】：-----" + needData);
                    int xss = Integer.parseInt(strings[4] + "", 16);
                    double value = BigDecimal.valueOf(xss).divide(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN).doubleValue();
                    System.out.println(new Date() + "水位：-----" + value + "℃");
                    int yss = Integer.parseInt(strings[6] + "", 16);
                    double value2 = BigDecimal.valueOf(yss).setScale(1, BigDecimal.ROUND_DOWN).doubleValue();
                    System.out.println(new Date() + "水位：-----" + value2 + "mm");
                    // 写入数据库，按照api方式发送指定服务上
//                    String url = "http://192.168.0.17:8888/api/geological/demo/insert";
//                    JSONObject object = new JSONObject();
//                    object.put("instrumentName", "水位计");
//                    object.put("value", needData);
//                    object.put("instrumentUid", 1);
//                    object.put("analyseValue", value2);
//                    object.put("type", "普通采集");
//                    String toString = object.toString();
//                    String post = CommonUtil.doHttpPost(url, toString);
//                    if (post.equals("0")) {
//                        System.out.println("传输数据失败");
//                    } else {
//                        System.out.println("传输数据成功");
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
    }
 
    /**
     * 字节数组转16进制字符串
     *
     * @param b 字节数组
     * @return 16进制字符串
     */
    public static String printHexString(byte[] b) {
        StringBuilder sbf = new StringBuilder();
        for (byte value : b) {
            String hex = Integer.toHexString(value & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sbf.append(hex.toUpperCase()).append(" ");
        }
        return sbf.toString().trim();
    }
 
    /**
     * 十六进制字符串转byte[]
     *
     * @param hex 十六进制字符串
     * @return byte[]
     */
    public static byte[] hexStr2Byte(String hex) {
        if (hex == null) {
            return new byte[]{};
        }
 
        // 奇数位补0
//        if (hex.length() % StaticConstant.TWO != 0) {
//            hex = "0" + hex;
//        }
 
        int length = hex.length();
        ByteBuffer buffer = ByteBuffer.allocate(length / 2);
        for (int i = 0; i < length; i++) {
            String hexStr = hex.charAt(i) + "";
            i++;
            hexStr += hex.charAt(i);
            byte b = (byte) Integer.parseInt(hexStr, 16);
            buffer.put(b);
        }
        return buffer.array();
    }
 
    /**
     * 16进制转换成为string类型字符串
     *
     * @param s 待转换字符串
     */
    public static String hexStringToString(String s) {
        if (s == null || "".equals(s)) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, StandardCharsets.UTF_8);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
 
}